package icu.guodapeng.oauth2.domain.user

import icu.guodapeng.oauth2.domain.user.dto.UserDto
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails

/**
 * 登录用户信息
 */
class SecurityUser(userDto: UserDto) : UserDetails {
    var id: Long = 0
    private var username: String = "" // 用户名
    private var password: String = "" // 用户密码
    private var enabled: Boolean = false // 用户状态
    private var authorities: Collection<SimpleGrantedAuthority> = ArrayList()

    init {
        this.id = userDto.id
        this.username = userDto.username
        this.password = userDto.password
        this.enabled = userDto.status == 1
        userDto.roles.forEach { item -> (authorities as ArrayList).add(SimpleGrantedAuthority(item)) }
    }

    override fun getAuthorities(): Collection<SimpleGrantedAuthority?> = authorities

    override fun getPassword(): String = password

    override fun getUsername(): String = username

    override fun isAccountNonExpired(): Boolean = true

    override fun isAccountNonLocked(): Boolean = true

    override fun isCredentialsNonExpired(): Boolean = true

    override fun isEnabled(): Boolean = enabled
}
